process.env.NODE_ENV = "production";
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const { merge } = require("webpack-merge");
const commonConfig = require("./webpack.common");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const SpeedMeasurePlugin = require("speed-measure-webpack-plugin");
const BundleAnalyzerPlugin =
  require("webpack-bundle-analyzer").BundleAnalyzerPlugin;

const isSMP = process.env.smp == "true";
const isAnalyze = process.env.analyze == "true";
const smp = new SpeedMeasurePlugin();

let _prodConfig = {
  mode: "production",
  stats: "errors-only",
  devtool: false,
  optimization: {
    usedExports: true,
    minimize: true,
    minimizer: [
      new CssMinimizerPlugin({
        include: "src",
      }),
      new TerserPlugin({
        parallel: true,
        extractComments: false,
        include: "src",
      }),
    ],
    splitChunks: {
      chunks: "all",
      minSize: 30000,
      // maxSize: 300000,
      minChunks: 1,
      maxAsyncRequests: 6,
      maxInitialRequests: 8,
      automaticNameDelimiter: "~",
      // name: true,
      cacheGroups: {
        locallibs: {
          test: /[\\/]node_modules[\\/](react|react-dom|redux|react-redux|react-router|react-router-dom|react-cropper|react-infinite-scroller|react-beautiful-dnd|moment|flv.js|crypto-js|cropperjs|clipboard|d3)[\\/]/,
          name: "locallibs",
          priority: -5,
        },
        localuis: {
          test: /[\\/]node_modules[\\/](antd)[\\/]/,
          name: "localuis",
          priority: -6,
        },
        localicons: {
          test: /[\\/]node_modules[\\/](@ant-design)[\\/]/,
          name: "localicons",
          priority: -7,
        },
        // localmobileuis: {
        //   test: /[\\/]node_modules[\\/](antd-mobile)[\\/]/,
        //   name: "localmobileuis",
        //   priority: -7,
        // },
        localcharts: {
          test: /[\\/]node_modules[\\/](echarts|echarts-for-react)[\\/]/,
          name: "localcharts",
          priority: -8,
        },
        localmaps: {
          test: /[\\/]node_modules[\\/](leaflet|leaflet-canvas-marker|leaflet.chinatmsproviders|leaflet.motion)[\\/]/,
          name: "localmaps",
          priority: -9,
        },
        vendors: {
          test: /[\\/]node_modules[\\/]/,
          name: "vendors",
          priority: -10,
        },
        default: {
          name: "default",
          minChunks: 2,
          priority: -20,
          reuseExistingChunk: true,
        },
      },
    },
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: "static/css/[name].[chunkhash:4].css",
    }),
    new CopyWebpackPlugin({
      patterns: [
        {
          from: "public/static/",
          to: "static/",
          // ignore: ["index.html", "config.js"],
        },
        {
          from: "release",
          to: "static/",
        },
      ],
    }),
    isAnalyze && new BundleAnalyzerPlugin(),
  ].filter(Boolean),
};

let prodConfig = merge(commonConfig, _prodConfig);

if (!isSMP) {
  module.exports = prodConfig;
} else {
  module.exports = smp.wrap(prodConfig);
}
